package com.qdlc.p2p.biz.processor.task.notices;

import java.text.MessageFormat;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.qd.p2p.adapter.AdaptedRequestHandleService;
import com.qdlc.p2p.biz.processor.event.BorrowRepayUpRateValueEvent;
import com.qdlc.p2p.biz.processor.event.RepayUpRateCollectNoticeEvent;
import com.qdlc.p2p.biz.processor.task.AbstractTaskProcessor;
import com.qdlc.p2p.biz.processor.task.pay.BorrowRepayUpRatePayTaskProcessor;
import com.qdlc.p2p.biz.service.CollectPayService;
import com.qdlc.p2p.common.constant.TradeStatus;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import com.qdlc.p2p.common.core.queue.TaskProcessorException;
import com.qdlc.p2p.common.core.queue.ValueEvent;
import com.qdlc.p2p.common.util.LogFactory;
import com.qdlc.p2p.dal.dto.TppTradeLog;

/**
 * 还款加息代收通知
 *
 * @author wuaj
 */
@Service
public class RepayUpRateCollectNoticeProcessor extends AbstractTaskProcessor {
    protected static final Logger logger = LogFactory.TRADE_LOGGER;

    @Resource
    @Qualifier("borrowRepayUpRateCollectService")
    private CollectPayService borrowRepayUpRateCollectService;

    @Resource
    @Qualifier("borrowRepayUpRatePayService")
    private CollectPayService borrowRepayUpRatePayService;

    @Resource
    private AdaptedRequestHandleService adaptedPayService;

    @Override
    public boolean supports(ValueEvent event) {
        return event instanceof RepayUpRateCollectNoticeEvent;
    }

    @Override
    public void process0(ValueEvent event) throws TaskProcessorException {
        RepayUpRateCollectNoticeEvent noticeEvent = (RepayUpRateCollectNoticeEvent) event;

        TradeStatus tradeStatus = noticeEvent.getTradeStatus(); // 交易状态
        double amount = noticeEvent.getDepositAmount(); // 投标金额
        TppTradeLog tppTradeLog = noticeEvent.getTppTradeLog(); // 交易log

        String tradeStatusStr = tradeStatus.getStatus();
        logger.debug(MessageFormat.format("S 还款加息代收通知处理 -> 订单号:{0} 交易状态:{1} 交易金额:{2}", tppTradeLog.getOrderNo(), tradeStatusStr, amount));
        // 已确定状态 处理业务逻辑
        if (tradeStatus == TradeStatus.TRADE_STATUS_FINISHED) {
            borrowRepayUpRateCollectService.tradeFinish(tppTradeLog, tradeStatusStr, amount);
            try {
                // 多笔代付
                List<TppTradeLog> tppTradeLogs = borrowRepayUpRatePayService.createOrders(tppTradeLog);
                // 代付放入队列中
                BorrowRepayUpRateValueEvent borrowRepayUpRateValueEvent = new BorrowRepayUpRateValueEvent();
                borrowRepayUpRateValueEvent.setTppTradeLogs(tppTradeLogs);
                borrowRepayUpRateValueEvent.setTaskProcessorClass(BorrowRepayUpRatePayTaskProcessor.class);
                //将事件放入到队列中
                QueueHandlers.put(borrowRepayUpRateValueEvent);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (tradeStatus == TradeStatus.TRADE_STATUS_FAILED) {
            borrowRepayUpRateCollectService.tradeFail(tppTradeLog, tradeStatusStr);
        }
        logger.debug(MessageFormat.format("E 还款加息代收通知处理 -> 订单号:{0} 交易状态:{1} 交易金额:{2}", tppTradeLog.getOrderNo(), tradeStatusStr, amount));
    }
}